package com.njym.java.security.admin.rest;


import com.github.pagehelper.PageInfo;
import com.njym.java.security.admin.biz.FamilyUserBiz;
import com.njym.java.security.admin.biz.IdentityApplyBiz;
import com.njym.java.security.admin.entity.FamilyUser;
import com.njym.java.security.admin.entity.IdentityApply;
import com.njym.java.security.admin.entity.IdentityApplyVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("identityApply")
public class IdentityApplyController extends BaseController<IdentityApplyBiz,IdentityApply> {
    @Autowired
    private FamilyUserBiz familyUserBiz;

    /**
     * 查询申请
     * @param name
     * @param mobile
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(value = "/queryList",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<IdentityApply> queryList(String name, String mobile,
        @RequestParam(defaultValue = "10") int limit,
        @RequestParam(defaultValue = "1")int page){
        List<IdentityApplyVo> list = baseBiz.queryList(name, mobile, page, limit);
        long total = PageInfo.of(list).getTotal();
        Map<String, Object> data = new HashMap<>(2);
        data.put("list", list);
        data.put("total", total);
        return new ObjectRestResponse<IdentityApply>().rel(true).data(data);

    }

    /**
     * 审核申请
     * @param id
     * @param status
     * @return
     */
    @RequestMapping(value = "/checkEducate",method = RequestMethod.GET)
    @ResponseBody
    public ObjectRestResponse<FamilyUser> checkEducate(String id, String status, String userId){
        if (id != null && id != "undefined" && status != null && status != "undefined" && userId != null && userId != "undefined"){
            Integer ids = Integer.parseInt(id);
            Integer sta = Integer.parseInt(status);
            Integer uId = Integer.parseInt(userId);
            IdentityApply identityApply = new IdentityApply();
            identityApply.setUserId(ids);
            identityApply.setStatus(sta);

            //同意时  如果未有课程码  则生成
            if(sta == 2){
                Example example = new Example(FamilyUser.class);
                example.createCriteria().andEqualTo("id", uId);
                List<FamilyUser> familyUser = familyUserBiz.selectByExample(example);
                if (familyUser.size() > 0){
                    FamilyUser u = familyUser.get(0);
                    if(u.getCourseCode() == null || "".equals(u.getCourseCode())){
                        //生成邀请码并判断是否重复
                        for(int i = 0; ; i ++) {
                            String code = "K" + genRandomNum();
                            Example example2 = new Example(FamilyUser.class);
                            example2.createCriteria().andEqualTo("courseCode", code);
                            List<FamilyUser> fu = familyUserBiz.selectByExample(example2);
                            if (fu.size() == 0) {
                                u.setCourseCode(code);
                                break;
                            }
                        }
                        familyUserBiz.updateById(u);
                    }
                }
            }
            baseBiz.updateSelectiveById(identityApply);
            return new ObjectRestResponse<IdentityApply>().rel(true);
        }else {
            return new ObjectRestResponse<IdentityApply>().rel(false);
        }

    }

    /**
     * 生成邀请码
     * @return
     */
    public String genRandomNum(){
        int  maxNum = 36;
        int i;
        int count = 0;
        char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while(count < 5){
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count ++;
            }
        }
        return pwd.toString();
    }
}